package cn.bob.org.service.impl;

import cn.bob.basic.query.NovelQuery;
import cn.bob.basic.utils.PageList;
import cn.bob.org.domain.Novel;
import cn.bob.org.mapper.NovelMapper;
import cn.bob.org.service.NovelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class NovelServiceImpl implements NovelService {

    @Autowired
    private NovelMapper novelMapper;

    @Override
    public void addNovel(Novel novel) {
        novelMapper.addNovel(novel);
    }

    @Override
    public void delNovel(Long id) {
        novelMapper.delNovel(id);
    }

    @Override
    public void updateNovel(Novel novel) {
        novelMapper.updateNovel(novel);
    }

    @Override
    public List<Novel> findAll() {
        return novelMapper.findAll();
    }

    @Override
    public Novel findById(Long id) {
        return novelMapper.findById(id);
    }

    @Override
    public PageList<Novel> queryByPage(NovelQuery novelQuery) {
        // 查询条数
        Integer count = novelMapper.queryCount(novelQuery);
        if (null == count || 0 == count) {
            return new PageList<>(new ArrayList<>(),0);
        }
        // 查询数据
        List<Novel> pagelist = novelMapper.queryByPage(novelQuery);
        return new PageList<>(pagelist, count);
    }
}
